package dbi.org.model;

import java.util.*;

import dbi.org.exception.DbNameDoesntMatchException;
import dbi.org.exception.ColumnDoesntExistsException;
import dbi.org.exception.ColumnNameEmptyException;
import dbi.org.exception.KeyColumnCannotBeEmptyException;
import dbi.org.exception.TableDoesntMatchException;

public class GroupKey {
	
	private String name;
	private HashMap<String, Column> columns;	
	protected String tableName;
	protected String dbName;
	
	
	public GroupKey(String theKeyName, String theTable, String theDatabase) {
		name = theKeyName;
		tableName = theTable;
		dbName = theDatabase;
		columns = new HashMap<String, Column>();
	}
	

	/**
	 * Veld toevoegen aan een primarykey
	 * @param column
	 * @throws DbNameDoesntMatchException 
	 * @throws TableDoesntMatchException 
	 * @throws KeyColumnCannotBeEmptyException 
	 */
	public void addColumn(Column column) throws DbNameDoesntMatchException, TableDoesntMatchException, KeyColumnCannotBeEmptyException {
		if(column.getDbName().equals(dbName)){
			if(column.getTableName().equals(tableName)){
				if(column.getMandatory() != false){ 
					//key kan niet null zijn
					columns.put(column.getName(), column);
				}else{
					throw new KeyColumnCannotBeEmptyException(column.getName());
				}
			}else{
				throw new TableDoesntMatchException("Tabelnaam komt niet overeen");
			}
		}else{
			throw new DbNameDoesntMatchException();
		}
		
	}
	
	public void addMultipleColumn(HashMap<String, Column> columns) throws DbNameDoesntMatchException, TableDoesntMatchException, KeyColumnCannotBeEmptyException {
		Iterator it = columns.entrySet().iterator();
		while(it.hasNext()) {
			Map.Entry<String,Column> pairs = (Map.Entry<String,Column>)it.next();
			this.addColumn(pairs.getValue());
		}
	}
	
	/**
	 * Veld verwijderen van een een primarykey
	 * @param veld
	 * @throws TableDoesntMatchException 
	 * @throws DbNameDoesntMatchException 
	 * @throws ColumnDoesntExistsException 
	 * @throws ColumnNameEmptyException 
	 */
	public void removeColumn(String columnName) throws ColumnDoesntExistsException, ColumnNameEmptyException {
		if (!columnName.equals("")) {
			if (columns.get(columnName) != null) {
				columns.remove(columnName);
			} else {
				throw new ColumnDoesntExistsException("Column bestaat niet");
			}
		} else {
			throw new ColumnNameEmptyException();
		}
	}
	
	public String getName() {
		return name;
	}


	public void setName(String keyName) {
		this.name = keyName;
	}


	
	public String getTableName() {
		return tableName;
	}


	public void setTableName(String tableName) {
		this.tableName = tableName;
	}


	public String getDbName() {
		return dbName;
	}


	public void setDbName(String dbName) {
		this.dbName = dbName;
	}


	public HashMap<String, Column> getColumns() {
		return columns;
	}
}
